<?php

namespace app\adminapi\controller;

use think\Controller;
use think\Request;
use \app\common\model\Auth as authmodel;

class auth extends BaseApi
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $params = input();
        $keyword = $params['keyword'];
        $type = $params['type'];
        $where = ['auth_name', 'like', '%%'];
        if (!empty($keyword)) {
            $where = ['auth_name', 'like', "%$keyword%"];
        }
        $list = authmodel::field('id,auth_name,pid,pid_path,auth_c,auth_a,is_nav,level')->where([$where])->select();
//        把查询到的数据变为标准的二维数组
        $list = (new \think\Collection($list))->toArray();
        if (!empty($type) && $type == 'tree') {
//            如果type=tree，返回树状结构数据
            $list = get_tree_list($list);
        } else {
//            返回无限极分类列表
            $list = get_cate_list($list);
        }
//        echo "<prev/>";
//        dump($list);
        $this->ok($list);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $params = input();
//        如果要添加的权限为顶级权限的话直接pid=0
        if (empty($params['pid'])) {
            $params['pid'] = 0;
        }
//        参数检测
        $validate = $this->validate($params, [
            'auth_name' => 'require',
            'pid' => 'require',
            'is_nav' => 'require'
        ]);
        if ($validate !== true) {
            $this->fail($validate);
        }

//        添加数据
        if ($params['pid'] == 0) {
            $params['level'] = 0;
            $params['pid_path'] = 0;
            $params['auth_c'] = '';
            $params['auth_a'] = '';
        } else {
//            根据要添加的权限的pid等于上一级的id，通过id查找上一级的level，然后要添加的权限的level=上一级的level+1
            $auth_info = authmodel::find($params['pid']);
            $params['level'] = $auth_info['level'] + 1;
            $params['pid_path'] = $auth_info['pid_path'] . '_' . $params['pid'];
        }
        $auth = authmodel::create($params, true);
        $data = authmodel::find($auth['id']);
        $this->ok($data);
    }

    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $params = input();
        $validate = $this->validate($params, [
            'auth_name' => 'require',
            'pid' => 'require',
            'is_nav' => 'require'
        ]);
        if ($validate != true) {
            $this->fail($validate);
        }
        $auth = AuthModel::find($params['id']);
        $auth->auth_name = $params['auth_name'];
        $auth->is_nav = $params['is_nav'];
        $auth->pid = $params['pid'];
        $auth->auth_c = $params['auth_c'];
        $auth->auth_a = $params['auth_a'];
        $res = $auth->allowField(true)->save();
        $data = AuthModel::find($params['id']);
        $this->ok($data);
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $params = input();
        $data = AuthModel::destroy($params['id']);
        $this->ok($data);
    }
}